package com.coocaa.ops.common.tool.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import com.coocaa.ops.common.tool.exception.BusinessException;
import com.coocaa.ops.common.tool.exception.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2018/9/5.
 * @description
 */
@Slf4j
public final class ParseJsonUtil {
    private static SerializeConfig mapping = new SerializeConfig();


    public static String obj2Json(Object data) {
        return JSON.toJSONString(data, mapping, new SerializerFeature[0]);
    }

    public static <T> T parse2EntityClass(String jsonStr, Class<T> classOfT) {
        try {
            return JSON.toJavaObject(JSON.parseObject(jsonStr), classOfT);
        } catch (Exception e) {
            Map errMap = InstanceUtil.newHashMap(2);
            errMap.put("jsonStr", jsonStr);
            errMap.put("className", classOfT.getSimpleName());
            throw new BusinessException(errMap, "Json串转化实体类失败");
        }
    }

    public static <T> List<T> parseArrayList(String jsonString, Class<?> cls) {
        ArrayList list = null;
        try {
            JSONArray jsonArray = JSON.parseArray(jsonString);
            if (jsonArray != null && jsonArray.size() > 0) {
                list = new ArrayList();

                for (int i = 0; i < jsonArray.size(); ++i) {
                    JSONObject object = jsonArray.getJSONObject(i);
                    Object model = JSONObject.toJavaObject(object, cls);
                    list.add(model);
                }
            }
            return list;
        } catch (Exception var7) {
            Map errMap = InstanceUtil.newHashMap(2);
            errMap.put("jsonStr", jsonString);
            errMap.put("className", cls.getSimpleName());
            throw new BusinessException(errMap, "Json串转化实体类列表-ArrayList失败");
        }

    }

    /**
     * 解析-JSONObject
     *
     * @param jsonStr
     * @return
     */
    public static JSONObject parseJSONObject(String jsonStr) {
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONObject.parseObject(jsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Assert.notNull(jsonObject, "解析的JSON不能为空！");
        return jsonObject;
    }

    /**
     * 解析-JSONArray
     *
     * @param jsonStr
     * @param key
     * @return
     */
    public static JSONArray parseJSONArray(String jsonStr, String key) {
        JSONObject jsonObject = parseJSONObject(jsonStr);
        return parseJSONArray(jsonObject, key);
    }

    /**
     * 解析-JSONArray
     *
     * @param jsonObject
     * @param key
     * @return
     */
    public static JSONArray parseJSONArray(JSONObject jsonObject, String key) {
        JSONArray items = jsonObject.getJSONArray(key);
        assertNotEmpty(items, key);
        return items;
    }


    public static String parseStringValue(JSONObject jsonObject, String key) {
        String value = jsonObject.getString(key);
        Assert.notNull(jsonObject, "解析的JSON字段值不能为空!,key:" + key);
        return value;
    }

    /**
     * 解析String数据-避免null值
     *
     * @param jsonObject
     * @param key
     * @return
     */
    public static String parseAvoidNullStringValue(JSONObject jsonObject, String key) {
        String value = jsonObject.getString(key);
        return value == null ? "" : value;
    }

    public static Integer parseIntValue(JSONObject jsonObject, String key) {
        Integer value = jsonObject.getInteger(key);
        Assert.notNull(value, "解析的JSON字段值不能为空!,key:" + key);
        return value;
    }

    public static String parseStringValue(String jsonStr, String key) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            String value = jsonObject.getString(key);
            return value;
        } catch (Exception e) {
            log.error(ExceptionUtil.getStackTraceAsString(e));
            return null;
        }
    }

    private static void assertNotEmpty(JSONArray jsonArray, String key) {
        if (jsonArray == null || jsonArray.size() == 0) {
            Map errMap = InstanceUtil.newHashMap(1);
            errMap.put("key", key);
            throw new BusinessException(errMap, "The corresponding parse value cannot be null");
        }
    }
}
